import 'dart:io';

import 'package:get/get.dart';
import 'package:xindong/common/xindong_app_info.dart';
import 'package:xindong/common/xindong_all.dart';
import 'package:xindong/common/xindong_methods.dart';
import 'package:xindong/common/xindong_storage.dart';
import 'package:xindong/manager/xindong_tencent_singleton.dart';
import 'package:xindong/models/xindong_yinsi.dart';
import 'package:xindong/plugins/xindong_to_native_plugin.dart';
import 'package:xindong/routes/call/controller/xindong_call_controller.dart';
import 'package:xindong/widgets/dialog/version/xindong_version_dialog.dart';
import 'package:umeng_common_sdk/umeng_common_sdk.dart';

class XinDongYinSiSingleton extends GetxController {
  static XinDongYinSiSingleton get to => Get.find();

  late XinDongYinSi agreement; // 同意对象
  bool isQuickPass = false; // 是否一键登录
  bool hadAgree = false; // 是否同意过了

  @override
  void onInit() {
    super.onInit();
    agreement = XinDongStorage.instance.getAgreement() ?? XinDongYinSi();
  }

  // 检测是否同意了协议
  bool checkAllPrivacyAgree() {
    if (isQuickPass == true) {
      if (agreement.agreePrivacy == true && agreement.agreeQuickPassPrivacy == true) {
        return true;
      }
    } else {
      if (agreement.agreePrivacy == true) {
        return true;
      }
    }
    // 显示提示
    showToast("需要同意协议才能进行下一步操作");
    // 抖动
    // animationShakeKey.currentState?.loadAnimation();
    // 震动
    // Vibrate.feedback(FeedbackType.warning);
    return false;
  }

  // 首次启动调用
  Future<bool> firstLaunch() async {
    if (agreement.agreePrivacy == true) {
      hadAgree = true;
      await _startInit();
    }
    return agreement.agreePrivacy;
  }

  // 同意协议时调用(已经同意过不生效)
  // 需要处理一大堆东西初始化去上报
  //
  // 注意：第二次打开应用不使用这个方法
  Future<void> _whenAgreeDoing() async {
    if (hadAgree) {
      return Future.value();
    }
    hadAgree = true;
    await _startInit();
    return Future.value();
  }

  // 友盟智能超链配置
  Future<void> _startInit() async {
    // 友盟
    UmengCommonSdk.initCommon(
      "653f116c58a9eb5b0af8e692",
      "60d5a55f8a102159db7c1956",
      Platform.isIOS ? "App Store" : XinDongAll.instance.androidChannel,
    );
    UmengCommonSdk.setPageCollectionModeAuto();

    // UMLink，官方建议延时1~2秒
    Future.delayed(const Duration(seconds: 2)).whenComplete(() {
      XinDongToNativePlugin.instance.initLink();
      XinDongToNativePlugin.instance.getAndroidOAID();
    });

    // 网络
    // XinDongNetworkConnectivity.instance.setupConnectivity();

    // 获取设备信息
    await XinDongAppDeviceInfo.instance.getDeviceInfo();

    // 腾讯
    await XinDongTencentSingleton.instance.setup();
    // 通话
    XinDongCallController.to.setup();
    // 版本检查
    XinDongVersionDialog.checkNeedUpgrade();
    return Future.value();
  }

  // 保存同意协议值
  void saveAgreement(XinDongYinSi model) {
    XinDongStorage.instance.saveAgreement(model);
  }

  // 隐私政策 - 同意就开始搞一堆事情
  //
  // withQuickPass 是不一键登录的
  // checkDisableHuawei 非华为渠道
  // 以上任意一项为true，则可以一起勾选同意，否则需要再一次勾选
  void changePrivacy({bool withQuickPass = false}) {
    // 一键登录的，或者非华为渠道的可以一起修改
    if (withQuickPass || Platform.isIOS) {
      agreement.agreeQuickPassPrivacy = !agreement.agreeQuickPassPrivacy;
      agreement.agreePrivacy = agreement.agreeQuickPassPrivacy;
    } else {
      agreement.agreePrivacy = !agreement.agreePrivacy;
    }
    if (agreement.agreePrivacy) {
      // 同意协议时调用
      _whenAgreeDoing();
    } else {
      agreement.agreeQuickPassPrivacy = false;
    }
    saveAgreement(agreement);
    update();
  }


  // 会员协议
  void changeHuiYuan() {
    agreement.agreeHuiYuan = !(agreement.agreeHuiYuan ?? false);
    saveAgreement(agreement);
    update();
  }
}
